/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.ExhaustFanLog;
import me.zhengjie.modules.biz.domain.FumeHood;
import me.zhengjie.modules.biz.domain.FumeHoodLog;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.repository.FumeHoodRepository;
import me.zhengjie.modules.biz.service.dto.ExhaustFanLogDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.FumeHoodLogRepository;
import me.zhengjie.modules.biz.service.FumeHoodLogService;
import me.zhengjie.modules.biz.service.dto.FumeHoodLogDto;
import me.zhengjie.modules.biz.service.dto.FumeHoodLogQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.FumeHoodLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-16
**/
@Service
@RequiredArgsConstructor
public class FumeHoodLogServiceImpl implements FumeHoodLogService {

    private final FumeHoodLogRepository fumeHoodLogRepository;
    private final FumeHoodLogMapper fumeHoodLogMapper;
    private final FumeHoodRepository fumeHoodRepository;

    @Override
    public Map<String,Object> queryAll(FumeHoodLogQueryCriteria criteria, Pageable pageable){
        Page<FumeHoodLog> page = fumeHoodLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<FumeHoodLogDto> dto = page.map(fumeHoodLogMapper::toDto);
        List<FumeHoodLogDto> resultDto = dto.getContent();
        for (FumeHoodLogDto fumeHoodLogDto:resultDto) {
            if (Objects.nonNull(fumeHoodLogDto.getFumeHoodId())) {
                FumeHood fumeHood = fumeHoodRepository.findById(fumeHoodLogDto.getFumeHoodId()).orElseGet(FumeHood::new);
                if (Objects.nonNull(fumeHood.getId())) {
                    fumeHoodLogDto.setName(fumeHood.getName());
                }
            }
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<FumeHoodLogDto> queryAll(FumeHoodLogQueryCriteria criteria){
        return fumeHoodLogMapper.toDto(fumeHoodLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FumeHoodLogDto findById(Integer id) {
        FumeHoodLog fumeHoodLog = fumeHoodLogRepository.findById(id).orElseGet(FumeHoodLog::new);
        ValidationUtil.isNull(fumeHoodLog.getId(),"FumeHoodLog","id",id);
        return fumeHoodLogMapper.toDto(fumeHoodLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FumeHoodLogDto create(FumeHoodLog resources) {
        return fumeHoodLogMapper.toDto(fumeHoodLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FumeHoodLog resources) {
        FumeHoodLog fumeHoodLog = fumeHoodLogRepository.findById(resources.getId()).orElseGet(FumeHoodLog::new);
        ValidationUtil.isNull( fumeHoodLog.getId(),"FumeHoodLog","id",resources.getId());
        fumeHoodLog.copy(resources);
        fumeHoodLogRepository.save(fumeHoodLog);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            fumeHoodLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FumeHoodLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FumeHoodLogDto fumeHoodLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("通风柜名称", fumeHoodLog.getName());
            map.put("通风柜ip", fumeHoodLog.getIp());
            map.put("排风量", fumeHoodLog.getExhaustAirRate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
